package com.xing.order.service.impl;

import com.xing.commonutils.user.CourseWebVoOrder;
import com.xing.commonutils.user.UcenterMemberOrder;
import com.xing.order.client.EduClient;
import com.xing.order.client.UcenterClient;
import com.xing.order.entity.TOrder;
import com.xing.order.mapper.TOrderMapper;
import com.xing.order.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.order.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xingfuao
 * @since 2022-04-08
 */

@Service
@Transactional //加上事务注解，支持ACID操作，失败后就回滚
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 根据课程id和用户id创建订单，返回订单id
     *
     * @param courseId
     * @param memberId
     * @return
     */

    //1.生成订单的方法
    @Override
    public String createOrders(String courseId, String memberId) {
        //远程调用---》根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //远程调用---》根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        System.out.println("userInfoOrder:"+userInfoOrder);

        //创建order对象，向order中设置需要的值
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        //课程封面图片路径
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);//0:未支付,这个只是生成订单，支付金钱过后，再去改这个订单的状态
        order.setPayType(1);//目前来说，只有微信可以支付，其他的暂时不做
        baseMapper.insert(order);
        //这是一个事务，中间出错的话，会回滚，这个不是多线程，只能按照顺序一步一步的执行下去的
        //返回订单号
        return order.getOrderNo();
    }
}
